import requests


class TestVerifyCode:
    # 类属性
    uuid = ""
    token = ""
    host = "http://kdtx-test.itheima.net"
    c_id = ""

    def test01_verify_code(self):
        # 2、调用请求方法，发送接口请求
        res = requests.get("http://kdtx-test.itheima.net/api/captchaImage")
        # 3、查看响应数据
        print(res.status_code)
        TestVerifyCode.uuid = res.json().get("uuid")
        try:
            # 4、断言
            assert res.json().get("msg") == f"操作成功", "断言失败，预期结果：操作成功 == 实际结果：{res.json().get('msg')}"
        except Exception as e:
            print("断言错误：", e)
            # 抛异常
            raise

    def test02_login(self):
        # 登录
        url_login = "http://kdtx-test.itheima.net/api/login"
        data = {
            "username": "admin",
            "password": "HM_2023_test",
            "code": "2",
            "uuid": TestVerifyCode.uuid
        }
        res = requests.post(url=url_login, json=data)
        # 断言登录结果
        assert "操作成功" == res.json().get("msg")
        # 提取token
        TestVerifyCode.token = res.json().get("token")
        print("token:", TestVerifyCode.token)

    def test03_Addcourse(self):
        url_addcourse = "http://kdtx-test.itheima.net/api/clues/course"
        data = {
            "name": "测试开发提升课01",
            "subject": "6",
            "price": 899,
            "applicablePerson": "2",
            "info": "测试开发提升课01"
        }
        res = requests.post(url=url_addcourse, json=data, headers={"Authorization": TestVerifyCode.token})
        assert "操作成功" == res.json().get("msg")
        TestVerifyCode.c_id = res.json().get("data").get("id")
        """4、查询课程接口"""

    def test04_query_course(self):
        url = TestVerifyCode.host + f"/api/clues/course/{TestVerifyCode.c_id}"
        res = requests.get(url, headers={"Authorization": TestVerifyCode.token})
        assert "操作成功" == res.json().get("msg")

    """5、修改课程接口"""

    def test05_update_course(self):
        url = TestVerifyCode.host + f"/api/clues/course/{TestVerifyCode.c_id}"
        data = {
            "name": "测试开发提升课02",
            "subject": "6",
            "price": 899,
            "applicablePerson": "2",
            "info": "测试开发提升课02"
        }
        res = requests.put(url, json=data, headers={"Authorization": TestVerifyCode.token})
        assert "操作成功" == res.json().get("msg")
        """6、删除课程接口"""

    def test06_delete_course(self):
        url = TestVerifyCode.host + f"/api/clues/course/{TestVerifyCode.c_id}"
        res = requests.delete(url, headers={"Authorization": TestVerifyCode.token})
        assert "操作成功" == res.json().get("msg")
